<?php


namespace app\api\common\card;

class Finance
{
	public static function set($card, $field, $number, $type, $remarks, $source)
	{
		if (!is_array($card)) {
			$card = \think\Db::name("app_card_user")->where(["id" => $card])->find();
		}
		$data = ["appid" => $card["appid"], "uid" => $card["id"], "field" => $field, "number" => $number, "type" => $type, "remarks" => $remarks, "after" => $type ? $card[$field] - $number : $card[$field] + $number, "ago" => $card[$field], "time" => time(), "source" => $source];
		$up = \think\Db::name("app_card_user")->where(["id" => $card["id"]]);
		if ($field == "endtime") {
			$endtime = $number;
			$data["after"] = $endtime;
			if ($card["endtime"] <= $endtime) {
				$data["number"] = $endtime - $card["endtime"];
				$data["type"] = 0;
			} else {
				$data["number"] = $card["endtime"] - $endtime;
				$data["type"] = 1;
			}
			\think\Db::name("app_card_user")->where("id", $card["id"])->update(["endtime" => $endtime]);
		} else {
			if ($type == 0) {
				$up->setInc($field, $number);
			} else {
				$up->setDec($field, $number);
			}
		}
		return \think\Db::name("app_card_user_finance_log")->insertGetId($data);
	}
}